﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace PeisongWeb.admin
{
    public partial class homeEdit :adminPage
    {
        protected int id
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["id"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { 
                ViewState["id"] = value; 
            }
        }
        protected string area
        {
            get
            {
                string tmp;
                try { tmp = ViewState["area"].ToString(); }
                catch { tmp = ""; }
                return tmp;
            }
            set
            { 
                ViewState["area"] = value; 
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("AH", 1))
                {
                    if (Request.QueryString["id"] != null)
                    {
                        if (Library.Number.IsIntPositive(Request.QueryString["id"]))
                        {
                            id = Convert.ToInt32(Request.QueryString["id"]);
                        }
                    }
                    if (Request.QueryString["area"] != null)
                    {
                        area = Request.QueryString["area"];
                    }
                    if (!string.IsNullOrEmpty(area))
                    {
                        showContor(area);
                        getDetails();
                    }  
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }

        private void showContor(string area)
        {
            if (area == "1")
            {
                li_img.Visible = true;
            }
            else
            {
                li_img.Visible = false;
            }
        }
        private void getDetails()
        {
            if (id != 0)
            {
                DAL.HomeEntity home = logic.home.getById(id);
                txtContent.Text = home.Text;
                txtUrl.Text = home.Url;
                chkIsShow.Checked = home.IsShow;
                txtSort.Text = home.Sort.ToString();

                if (home.FileName != null)
                {
                    imgCurrent.ImageUrl = Library.Resource.UploadPath + Library.Resource.FileOriginal + "/" + home.FileName;
                    imgCurrent.AlternateText = "";
                    hdCurrentImageFileName.Value = home.FileName;
                }
                else
                {
                    imgCurrent.Visible = false;
                }
            }
            else
            {
                imgCurrent.Visible = false;
            }
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (fileImage.HasFile)
            {
                if (!Library.FileLib.isPicByFileName(fileImage.PostedFile.FileName))
                {
                    Library.Script.ClientMsg("无效的图片，请重新选择.");
                    return;
                }
            }

            #region 保存信息
            DAL.HomeEntity home = new DAL.HomeEntity();
            home.Url = txtUrl.Text;
            home.Area = area;
            home.IsShow = chkIsShow.Checked;
            home.Text = txtContent.Text;
            int sort = 0;
            int.TryParse(txtSort.Text, out sort);
            home.Sort = sort;

            if (id != 0)
            {
                if (fileImage.HasFile)
                {
                    if (hdCurrentImageFileName.Value != "")
                    {
                        try
                        {
                            Library.FileLib.Del(Library.Resource.UploadPath + Library.Resource.FileOriginal + "/" + hdCurrentImageFileName.Value);
                        }
                        catch
                        {
                        }
                    }
                    home.FileName = Library.ImgLib.getUploadFilenameNotMakeThumb(fileImage.PostedFile);
                }
                else
                {
                    home.FileName = hdCurrentImageFileName.Value;
                }
                home.HomeId = id;
                logic.home.update(home);
            }
            else
            {
                if (fileImage.HasFile)
                {
                    home.FileName = Library.ImgLib.getUploadFilenameNotMakeThumb(fileImage.PostedFile);
                }
                logic.home.insert(home);
            }
            #endregion

            Library.Script.ClientMsgUrl("保存成功！", "homeList.aspx");
        }
    }
}
